MODULE datamm
  USE GLOBVAR
  USE statistics
  !USE INTEGRATION
  IMPLICIT NONE
  
CONTAINS
  
  SUBROUTINE datam(momin)
    IMPLICIT NONE
    REAL(8), INTENT(INOUT):: momin(:) !initial guess
    REAL(8) :: a(N_ind,n_testf)
    REAL(8) :: inter(n_ind,n_levelf,20,3),interf(n_ind,n_levelf,3)
    REAL(8),ALLOCATABLE:: AVSE(:),cor(:,:)
    INTEGER :: du(n_levelf),ind(N_ind,n_levelf)
    INTEGER :: i,j,k,t,m,n,l,indexf(n_testf,2), artf(n_ind,n_levelf,6),con(n_mom),mmm,tt,kk,mn,nm 
    
    !!!!! start to calculate data moments!!!!!
    
    !!! cognitive tasks!!!!
    indexf=0
    DO i=1,N_ind
      DO j=1,N_testf
         IF (f_test(i,j)>-90) THEN
             indexf(j,1)=indexf(j,1)+1
             IF (f_test(i,j)>0.5) indexf(j,2)=indexf(j,2)+1
         END IF
      END DO
    END DO
    
    DO i=1,N_testf
        IF (indexf(i,1)>0.5) THEN
            momin(i)=DBLE(indexf(i,2))/DBLE(indexf(i,1)) ! PASSING RATE FOR EACH Cognitive TASK
            con(i)=indexf(i,1)
        ELSE 
            momin(i)=-99
            con(i)=-99
        END IF
    END DO
    
    !!!! generate artf variables !!!!
    artf=-1
    DO i=1,N_ind
        DO j=1,N_testf
            IF (f_test(i,j)>-90) THEN
                k=levelf(j)
                m=repf(i,j)
                artf(i,k,m)=f_test(i,j)
                inter(i,k,m,1:3)=x_testf(i,j,1:3)
            END IF
        END DO
    END DO
    
    !!!! passing rate at each level!!!!

    DO j=N_testf+1,N_testf+N_levelf
       mn=0
       nm=0 
       DO i=1,N_ind
                      m=0
                             n=0
           DO k=1,6
               IF (artf(i,j-N_testf,k)>-0.5) THEN
                   m=m+1
                                      mn=mn+1
                   IF (artf(i,j-N_testf,k)==1) THEN
                                                           n=n+1
                                            nm=nm+1
                                                     END IF
               END IF
           END DO
           IF (m>0) THEN
               ave(i,j-N_testf)=DBLE(n)/DBLE(m)
           ELSE
               ave(i,j-N_testf)=-99
           END IF
 
       END DO
       IF (mN>0) THEN
           momin(j)=DBLE(nm)/DBLE(mn)
           con(j)=m
       ELSE
           momin(j)=-99
           con(j)=-99
       END IF
    END DO
    
    !!!! correlation across levels !!!!
    
    mmm=N_testf+N_levelf
    
   l=0 
   DO j=1,N_levelf-1
       IF (j==1) THEN
           tt=5
           kk=2
       ELSE IF (j==2) THEN
           tt=2
           kk=5
       ELSE IF (j==3) THEN
           tt=5
           kk=5
       ELSE IF (j==4) THEN
           tt=5
           kk=3
       ELSE IF(j==5) THEN
           tt=3
           kk=4
       ELSE IF (j==6) THEN
           tt=4
           kk=3
       END IF
      DO t=1,tt
        DO k=1,kk
          m=0
          n=0
          l=l+1
          DO i=1,N_ind 
             IF (artf(i,j,t)==1 .and. artf(i,j+1,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
                 m=m+1
                 IF (artf(i,j,t)==1 .and. artf(i,j+1,k)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momin(mmm+l)=DBLE(n)/DBLE(m)
             con(mmm+l)=m
          ELSE
             momin(mmm+l)=-99
             con(mmm+l)=-99
          END IF
        END DO
      END DO
   END DO
    
    mmm=N_testf+N_levelf+84
    
    l=0
    DO j=1,N_levelf
        
       IF (j==1) THEN
           tt=4
           kk=5
       ELSE IF (j==2) THEN
           tt=1
           kk=2
       ELSE IF (j==3) THEN
           tt=4
           kk=5
       ELSE IF (j==4) THEN
           tt=4
           kk=5
       ELSE IF(j==5) THEN
           tt=2
           kk=3
       ELSE IF (j==6) THEN
           tt=3
           kk=4
       ELSE IF (j==7) THEN
           tt=2
           kk=3
       END IF
        
      DO t=1,tt
        DO k=t+1,kk
          m=0
          n=0
          l=l+1
          DO i=1,N_ind 
             IF (artf(i,j,t)==1 .and. artf(i,j,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
                 m=m+1
                 IF (artf(i,j,t)==1 .and. artf(i,j,k)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momin(mmm+l)=DBLE(n)/DBLE(m)
             con(mmm+l)=m
          ELSE
             momin(mmm+l)=-99
             con(mmm+l)=-99
          END IF
        END DO
      END DO
    END DO
    
     mmm=N_testf+N_levelf+84+43
    l=0
    DO j=1,N_levelf-2
        IF (j==1) THEN
            tt=5
            kk=5
        ELSE IF (j==2) THEN
            tt=2
            kk=5
        ELSE IF (j==3) THEN
            tt=5
            kk=3
        ELSE IF (j==4) THEN
            tt=5
            kk=4
        ELSE IF (j==5) THEN
            tt=3
            kk=3
        END IF
        
      DO t=1,tt
        DO k=1,kk
          m=0
          n=0 
          l=l+1
          DO i=1,N_ind 
             IF (artf(i,j,t)==1 .and. artf(i,j+2,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
                 m=m+1
                 IF (artf(i,j,t)==1 .and. artf(i,j+2,k)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momin(mmm+l)=DBLE(n)/DBLE(m)
             con(mmm+l)=m
          ELSE
             momin(mmm+l)=-99
             con(mmm+l)=-99
          END IF
        END DO
      END DO
    END DO  
    
     mmm=N_testf+N_levelf+84+43+79
    
    
    indexf=0
    DO i=1,N_ind
      DO j=1,N_testf
          a(i,j)=firstf(i,2)-monthf(j)-DBLE(timef(j))/4.0d0
         IF (f_test(i,j)>-90 .and. a(i,j)>-1.25d0) THEN
             indexf(j,1)=indexf(j,1)+1
             IF (f_test(i,j)>0.5) indexf(j,2)=indexf(j,2)+1
         END IF
      END DO
    END DO
    
    DO i=1,N_testf
        IF (indexf(i,1)>0.5) THEN
            momin(mmm+i)=DBLE(indexf(i,2))/DBLE(indexf(i,1)) ! PASSING RATE FOR EACH LANGUAGE TASK
            con(mmm+i)=indexf(i,1)
        ELSE 
            momin(mmm+i)=-99
            con(mmm+i)=-99
        END IF
    END DO
    
    mmm=2*N_testf+N_levelf+84+43+79
    
    indexf=0
    DO i=1,N_ind
      DO j=1,N_testf
          a(i,j)=DBLE(firstf(i,2)-monthf(j)-DBLE(timef(j))/4.0d0)
         IF (f_test(i,j)>-90 .and. a(i,j)<-1.25d0) THEN
             indexf(j,1)=indexf(j,1)+1
             IF (f_test(i,j)>0.5) indexf(j,2)=indexf(j,2)+1
         END IF
      END DO
    END DO

    
    
    DO i=1,N_testf
        IF (indexf(i,1)>0.5) THEN
            momin(mmm+i)=DBLE(indexf(i,2))/DBLE(indexf(i,1)) ! PASSING RATE FOR EACH LANGUAGE TASK
            con(mmm+i)=indexf(i,1)
        ELSE 
            momin(mmm+i)=-99
            con(mmm+i)=-99
        END IF
    END DO
    
    mmm=3*N_testf+N_levelf+84+43+79
    
    l=0
    DO j=1,N_levelf-1
      DO t=1,5
          m=0
          n=0
          l=l+1
          DO i=1,N_ind 
             IF (artf(i,j,t)>-0.5) THEN   
                 m=m+1
                 IF (artf(i,j,t)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momin(mmm+l)=DBLE(n)/DBLE(m)
             con(mmm+L)=m
          ELSE
             momin(mmm+l)=-99
             con(mmm+L)=-99
          END IF
      END DO
    END DO
    mmm=3*N_testf+N_levelf+84+43+79+5*(N_levelf-1)  
    
    
    
    DUf=-99
    ind=-99
    DO i=1,N_ind
        DO k=1,N_levelf
            DO j=1,6
                IF (artf(i,k,j)==1 .and. j==1) THEN
                    duf(i,k)=1
                    ind(i,k)=1
                ELSE IF (artf(i,k,j)==0 .and. j==1) THEN
                    duf(i,k)=j
                    ind(i,k)=0
                ELSE IF (j>1 ) THEN
                    IF (artf(i,k,j-1)==0 .and. artf(i,k,j)==0 .and. ind(i,k)==0) THEN
                      duf(i,k)=j
                       ind(i,k)=0
                       IF (j<6) THEN
                           IF (artf(i,k,j+1)==-1) THEN
                           duf(i,k)=j+1
                           ind(i,k)=1
                           END IF
                       END IF                      
                           
                    ELSE IF (artf(i,k,j-1)==0 .and. artf(i,k,j)==0 .and. ind(i,k)==1) THEN
                        duf(i,k)=duf(i,k)
                        ind(i,k)=1
                    ELSE IF (artf(i,k,j-1)==0 .and. artf(i,k,j)==1 .and. ind(i,k)==0) THEN
                        duf(i,k)=j
                        ind(i,k)=1
                    ELSE IF (artf(i,k,j-1)==0 .and. artf(i,k,j)==1 .and. ind(i,k)==1) THEN
                        duf(i,k)=duf(i,k)
                        ind(i,k)=1
                    END IF
                END IF
            END DO           
                
        END DO
    END DO
    
    DO k=1,n_levelf
        m=0
        n=0
        DO i=1,n_ind
            IF (duf(i,k)>-90) THEN
               m=m+1
               n=n+duf(i,k)
            END IF
        END DO
        IF (m>0) THEN
          momin(mmm+k)=DBLE(n)/DBLE(m)
          con(mmm+k)=1
        ELSE 
           momin(mmm+k)=-99
           con(mmm+k)=-99
        END IF
    END DO
    
    mmm=3*N_testf+N_levelf+84+43+79+5*(N_levelf-1)+n_levelf  
    
    du=0
    DO k=1,n_levelf
        DO i=1,N_ind
            IF (duf(i,k)>-90) THEN
                du(k)=du(k)+1
            END IF
        END DO
    END DO
    DO k=1,n_levelf
        allocate(avse(du(k)))
        m=0
        DO i=1,n_ind
            IF (duf(i,k)>-90) THEN
                m=m+1
                avse(m)=duf(i,k)
            END IF
        END DO
        momin(mmm+k)=STDEV_V(AVSE)
        CON(MMM+K)=1
        DEALLOCATE(avse)
    END DO
    mmm=3*N_testf+N_levelf+84+43+79+5*(N_levelf-1)+2*n_levelf 
    

    IND=0
    interf=0.0d0
    DO k=1,N_levelf
        DO i=1,N_ind
          DO j=1,6
              IF (inter(i,k,j,1)>-90) THEN
                  ind(i,k)=ind(i,k)+1
                  interf(i,k,1)=interf(i,k,1)+inter(i,k,j,1)
                  interf(i,k,2)=interf(i,k,2)+inter(i,k,j,2)
                  interf(i,k,3)=interf(i,k,3)+inter(i,k,j,3)
              END IF
          END DO
          IF (ind(i,k)>0) THEN
               interf(i,k,1)=DBLE(interf(i,k,1))/DBLE(ind(i,k))
               interf(i,k,2)=DBLE(interf(i,k,2))/DBLE(ind(i,k))
               interf(i,k,3)=DBLE(interf(i,k,3))/DBLE(ind(i,k))
          END IF
        END DO
    END DO
    
    
    du=0
    DO k=1,n_levelf
        DO i=1,N_ind
            IF (ind(i,k)>0 .AND. duf(i,k)>-90) THEN
            du(k)=du(k)+1          
            END IF
        END DO
    END DO
    
    DO k=1,n_levelf
        allocate(cor(du(k),4))
        m=0
        DO i=1,n_ind
             IF (ind(i,k)>0 .AND. duf(i,k)>-90) THEN
                m=m+1
                cor(m,1)=interf(i,k,1)
                cor(m,2)=interf(i,k,2)
                cor(m,3)=interf(i,k,3)
                cor(m,4)=duf(i,k)
             END IF
        END DO
        !IF (k>1) THEN
        momin(mmm+3*(k-1)+1)=Correlation(cor(:,1),cor(:,4))
        momin(mmm+3*(k-1)+2)=Correlation(cor(:,2),cor(:,4))
        momin(mmm+3*(k-1)+3)=Correlation(cor(:,3),cor(:,4))
        CON(MMM+3*(k-1)+1)=1
        CON(MMM+3*(k-1)+2)=1
        CON(MMM+3*(k-1)+3)=1
        !ELSE 
        !    momin(mmm+3*(k-1)+1)=-99
        !    momin(mmm+3*(k-1)+2)=-99
        !    momin(mmm+3*(k-1)+3)=-99
        !    CON(MMM+3*(k-1)+1)=-99
        !    CON(MMM+3*(k-1)+2)=-99
        !    CON(MMM+3*(k-1)+3)=-99
        !END IF
        DEALLOCATE(cor)
    END DO
   
    
    countnum=con
      
        
    
    
    
    
  END SUBROUTINE datam
  
END MODULE datamm